/**
 * 报表设计公司(2011)
 */
package com.reportgear.designer.ui.base;

import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;

import com.reportgear.report.core.api.idname.IdName;

/**
 * @version 1.0 2011-3-24
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ANNCSR 2.0
 */
public class JIdNameComboBox<T> extends JComboBox {

	JIdNameComboBox() {
		super();
	}

	public JIdNameComboBox(final Object items[]) {
		super(items);
	}

	public JIdNameComboBox(ComboBoxModel model) {
		super(model);
	}

	/**
	 * 根据id设置当前选择项，Model包含的对象必须为IIdName
	 * 
	 * @param id
	 *            id值
	 */
	public void setIndexByValueId(T id) {
		ComboBoxModel model = getModel();
		int size = model.getSize();

		for (int i = 0; i < size; i++) {
			if (((IdName<T>) model.getElementAt(i)).getId() == id) {
				setSelectedIndex(i);
				return;
			}
		}
	}

	/**
	 * 获取选择的Item中的Id值,
	 * 
	 * @return Id,如果没有选择则返回null
	 */
	public T getSelectValueId() {
		Object o = getSelectedItem();
		if (o != null) {
			return ((IdName<T>) o).getId();
		} else {
			return null;
		}
	}
}
